package com.itheima.netty.test07;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class EchoClientHandler extends ChannelInboundHandlerAdapter {

	private int count;

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		
		User[] users = getUsers();
		for (User u : users) {
			ctx.write(u);
		}
		ctx.flush();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("this is client receive msg【  " + ++count + "  】times:【" + msg + "】");
		if (count < 5) {
			ctx.write(msg);
		}
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}

	private User[] getUsers() {
		User[] users = new User[5];
		for (int i = 0; i < 5; i++) {
			User user = new User();
			user.setId(String.valueOf(i));
			user.setAge(18 + i);
			user.setName("张元" + i);
			user.setSex("男" + String.valueOf(i * 2));
			users[i] = user;
		}
		return users;
	}
}
